<?php
include "exception.php";
include "db.php";
class SSKSQLiteException extends SSKCustomException{};
function throw_SSKSQLiteException($message = null, $code = 0) {
	throw new SSKSQLiteException($message, $code);
}
class SQLite implements IDatabase {
	#TODO: un po' tutto, che poi manco so usare SQLite. (su tutto, le varie Fetch*())
	private $filename = null;
	private $mode = null;
	private $db = null;

	private $connected = false;
	
	function __construct($filename,$mode) {
		$this->filename=$filename;
		$this->mode=$mode;
	}
	function Connect() {
		if(!$this->connected) {
			$this->db = sqlite_open($this->filename, $this->mode, $errstr);
			if(!$this->db) {
				throw new SSKSQLiteException("Can't open database: ".$errstr);
			}
			$this->connected=true;
		} else {
			throw new SSKSQLiteException("Database already opened.");
		}
	}
	function Disconnect() {
		if($this->connected) {
			if(sqlite_close($this->db)) {
				$this->connected=false;
			} else {
				throw new SSKSQLiteException("Can't close database.");
			}
		} else {
			throw new SSKSQLiteException("No database opened.");
		}
	}
	function Query($sql) {
		if($this->connected) {
			$sql=sqlite_query($this->db, $sql, SQLITE_BOTH, $errstr);
			if(!$sql) {
				throw new SSKSQLiteException("Can't query database: ".$errstr);
			}
			return $sql;
		} else {
			throw new SSKSQLiteException("No database opened.");
		}
	}
}
?>